Imports DotNetNuke.Entities.Portals
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Entities.Tabs
Imports DotNetNuke.Security
Imports System.Linq

Namespace RTP.HTMLNewsletter
    Public MustInherit Class Settings
        Inherits Entities.Modules.ModuleSettingsBase

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub

        'The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object

        Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub

#End Region
#Region "Private Data"
#End Region

#Region "Help Methods"
        Public Overrides Sub LoadSettings()
            ' Do we have to load the settings?
            If Not Page.IsPostBack Then
                If CType(Settings("ignoreemail"), String) <> "" Then
                    Me.txtIgnoreEmail.Text = CType(Settings("ignoreemail"), String)
                Else
                    Me.txtIgnoreEmail.Text = ""
                End If
                If CType(Settings("profilefield"), String) <> "" Then
                    Me.txtProfileField.Text = CType(Settings("profilefield"), String)
                Else
                    Me.txtProfileField.Text = ""
                End If
                If CType(Settings("restrictemail"), String) <> "" Then
                    Me.chkRestrictEmail.Checked = CType(Settings("restrictemail"), Boolean)
                Else
                    Me.chkRestrictEmail.Checked = True
                End If
                If CType(Settings("enablesocial"), String) <> "" Then
                    Me.chkEnableSocial.Checked = CType(Settings("enablesocial"), Boolean)
                Else
                    Me.chkEnableSocial.Checked = False
                End If

                Dim DisplayTabModule As String = Nothing
                If CType(Settings("displaytabmodule"), String) <> "" Then
                    DisplayTabModule = CType(Settings("displaytabmodule"), String)
                End If
                LoadTabModules(DisplayTabModule)

            End If

        End Sub

        Public Overrides Sub UpdateSettings()
            Dim objModules As New DotNetNuke.Entities.Modules.ModuleController

            Try
                objModules.UpdateModuleSetting(ModuleId, "ignoreemail", txtIgnoreEmail.Text.Trim.ToString())
                objModules.UpdateModuleSetting(ModuleId, "profilefield", txtProfileField.Text.Trim.ToString())
                objModules.UpdateModuleSetting(ModuleId, "restrictemail", chkRestrictEmail.Checked.ToString())
                objModules.UpdateModuleSetting(ModuleId, "enablesocial", chkEnableSocial.Checked.ToString())
                objModules.UpdateModuleSetting(ModuleId, "displaytabmodule", cboDisplayTabModule.SelectedValue)

            Catch exc As Exception 'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try

        End Sub
#End Region

#Region "Private Methods"
        Private Sub LoadTabModules(ByVal DisplayTabModule As String)
            Dim FriendlyName As String = DesktopModuleController.GetDesktopModuleByModuleName("HTMLNewsletter", PortalId).FriendlyName
            Dim objModuleController As New ModuleController
            Dim objTabController As New TabController
            Dim objTabModules As ArrayList = objModuleController.GetModulesByDefinition(PortalId, FriendlyName)
            Dim TabModules As New Generic.List(Of ListItem)
            cboDisplayTabModule.Items.Clear()
            For Each objModule As ModuleInfo In objTabModules
                Dim TabName As String = objTabController.GetTab(objModule.TabID, PortalId, False).TabName
                TabModules.Add(New ListItem(TabName & " - " & objModule.ModuleTitle, CStr(objModule.TabModuleID)))
            Next
            cboDisplayTabModule.DataSource = TabModules.Cast(Of ListItem).OrderBy(Function(o) o.Text)
            cboDisplayTabModule.DataTextField = "Text"
            cboDisplayTabModule.DataValueField = "Value"
            cboDisplayTabModule.DataBind()
            If Not DisplayTabModule Is Nothing Then
                cboDisplayTabModule.ClearSelection()
                cboDisplayTabModule.Items.FindByValue(DisplayTabModule).Selected = True
            Else
                Dim TabModuleID As Integer = objModuleController.GetModule(ModuleId, TabId, False).TabModuleID
                cboDisplayTabModule.ClearSelection()
                cboDisplayTabModule.Items.FindByValue(CStr(TabModuleID)).Selected = True
            End If
        End Sub
#End Region

    End Class

End Namespace